package com.learning.hadoop.mapreduce.weather;

import com.google.common.base.Objects;
import lombok.Getter;
import lombok.Setter;
import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * ClassName: KeyPari
 * Description: 定义的key
 * Date: 2018/3/25 21:38 【需求编号】
 *
 * @author Sam Sho
 * @version V1.0.0
 */
@Setter
@Getter
public class KeyPari implements WritableComparable<KeyPari> {
    private int year;
    private int hot;

    @Override
    public void write(DataOutput out) throws IOException {
        out.writeInt(this.year);
        out.writeInt(this.hot);
    }

    @Override
    public void readFields(DataInput in) throws IOException {
        this.year = in.readInt();
        this.hot = in.readInt();
    }

    @Override
    public int compareTo(KeyPari o) {
        int compare = Integer.compare(year, o.getYear());
        return compare == 0 ? Integer.compare(hot, o.getHot()) : compare;
    }

    @Override
    public String toString() {
        return Objects.toStringHelper(this)
                .add("year", year)
                .add("hot", hot)
                .toString();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        KeyPari keyPari = (KeyPari) o;
        return year == keyPari.year &&
                hot == keyPari.hot;
    }

    @Override
    public int hashCode() {
        return java.util.Objects.hash(year, hot);
    }
}